package oct2013;

import java.util.StringTokenizer;

/**
 * @author Yinzi Chen
 * @date Nov 7, 2013
 */
public class SimplifyPath {
    public String simplifyPath(String path) {
        StringTokenizer st = new StringTokenizer(path, "/");
        int n = st.countTokens(), i = 0, j = 0;
        String[] str = new String[n];
        while (st.hasMoreTokens()) {
            str[i++] = st.nextToken();
        }
        i = 0;
        while (j < n) {
            if (str[j].equals("..")) {
                if (i != 0) {
                    i--;
                }
            } else if (!str[j].equals(".")) {
                str[i++] = str[j];
            }
            j++;
        }
        if (i == 0)
            return "/";
        StringBuilder sb = new StringBuilder();
        for (j = 0; j < i; ++j) {
            sb.append('/');
            sb.append(str[j]);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        SimplifyPath a = new SimplifyPath();
        System.out.println(a.simplifyPath("/bcsdas/../../a/b"));
    }
}
